package teste;

import java.io.InputStream;

import br.affCorp.controller.rdf.impl.UsuarioRDF;
import br.affCorp.model.ImagemPerfilDTO;
import br.affCorp.model.UsuarioDTO;
import br.affCorp.util.vocabulary.rdf.LOM;
import br.affCorp.util.vocabulary.rdf.SIOC;
import br.util.AFFCorpWebUtil;
import br.util.AppJsfUtil;
import br.util.AppEnums.Arquivo;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.sparql.core.ResultBinding;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;
import com.hp.hpl.jena.util.FileManager;

public class ExemploJena {

	public InputStream obterArquivoRDF(String dominio) {

		String caminho = "Z:\\java\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\AFFCorp\\data\\alabeduarte\\";

		String inputFileName = caminho + dominio + Arquivo.RDF.getExtensao();

		InputStream in = FileManager.get().open(inputFileName);

		return in;
	}

	public String obterValorDaPropriedade(Resource resource, Property property) {
		Statement statement;

		statement = resource.getProperty(property);

		if (!AppJsfUtil.isNullOrEmpty(statement)) {
			return statement.getString();
		}

		return null;
	}

	private UsuarioDTO obterUsuario(Resource resource, UsuarioDTO usuarioDTO) {

		String login = obterValorDaPropriedade(resource, FOAF.accountName);
		String nick = obterValorDaPropriedade(resource, FOAF.nick);
		String nome = obterValorDaPropriedade(resource, FOAF.firstName);
		String sobreNome = obterValorDaPropriedade(resource, FOAF.surname);
		String email = obterValorDaPropriedade(resource, FOAF.mbox);
		String caminhoImagemPerfil = obterValorDaPropriedade(resource, FOAF.img);

		if (!AFFCorpWebUtil.isNullOrEmpty(login)) {
			if (AFFCorpWebUtil.isNullOrEmpty(usuarioDTO.getLogin())) {
				usuarioDTO.setLogin(login);
			}
		}
		if (!AFFCorpWebUtil.isNullOrEmpty(nick)) {
			if (AFFCorpWebUtil.isNullOrEmpty(usuarioDTO.getNickName())) {
				usuarioDTO.setNickName(nick);
			}
		}
		if (!AFFCorpWebUtil.isNullOrEmpty(nome)) {
			if (AFFCorpWebUtil.isNullOrEmpty(usuarioDTO.getNome())) {
				usuarioDTO.setNome(nome);
			}
		}
		if (!AFFCorpWebUtil.isNullOrEmpty(sobreNome)) {
			if (AFFCorpWebUtil.isNullOrEmpty(usuarioDTO.getSobreNome())) {
				usuarioDTO.setSobreNome(sobreNome);
			}
		}
		if (!AFFCorpWebUtil.isNullOrEmpty(email)) {
			if (AFFCorpWebUtil.isNullOrEmpty(usuarioDTO.getEmail())) {
				usuarioDTO.setEmail(email);
			}
		}
		if (!AFFCorpWebUtil.isNullOrEmpty(caminhoImagemPerfil)) {
			usuarioDTO.setImagemPerfil(new ImagemPerfilDTO());
			usuarioDTO.getImagemPerfil().setUrl(caminhoImagemPerfil);
		}

		return usuarioDTO;
	}

	public static void main(String args[]) {

		Model model = ModelFactory.createDefaultModel();

		ExemploJena exemploJena = new ExemploJena();

		model.read(exemploJena.obterArquivoRDF("10886451842774632785021220090217"), "");

		StringBuffer querySB = new StringBuffer();

		String id = "<http://affCorp.com.br/alabeduarte/10886451842774632785021220090217>";

		querySB.append(" PREFIX sioc: <" + SIOC.getURI() + "> 		");
		querySB.append(" PREFIX lom: <" + LOM.getURI() + "> 		");
		querySB.append(" SELECT 									");
		querySB.append(" 	?titulo	 								");
		querySB.append(" 	?descricao	 								");
		querySB.append(" 	?oa	 								");
		querySB.append(" WHERE	 									");
		querySB.append(" 	{	 									");
		 querySB.append("  		?oa lom:description ?descricao		.	  		");
		 querySB.append("  		?oa lom:title ?titulo		.	  		");
		querySB.append("  		FILTER (?oa = " + id + ")								");
		querySB.append(" 	}										");

		
		Query query = QueryFactory.create(querySB.toString());

		QueryExecution qe = QueryExecutionFactory.create(query, model);
		try {
			ResultSet results = qe.execSelect();
			ResultSetFormatter.out(System.out, results, query);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			qe.close();
		}

	}
}
